#! /usr/bin/env python
# -*- coding: latin-1 -*-
from gui.MainWindow import Ui_MainWindow 
from gui.OpenFileDialog import ImageOpenFileDialog, ImageOpenFileDialogRecursive
from ImageRep import Image
from Repository import Repository
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import Config
import sys


class MainGui(QMainWindow):

	def __init__(self):
		QMainWindow.__init__(self)

		self.ui = Ui_MainWindow()
		self.ui.setupUi(self)

		self.setupThumbViewer()

		self.setupConnectors()
		self.repo = Repository()



	def setupConnectors(self):
		self.connect(self.ui.openFileDialog,SIGNAL("triggered()"), self.openImagesFileDialog)
		self.connect(self.ui.openRecursive,SIGNAL("triggered()"), self.openImagesRecursiveFileDialog)
		self.connect(self.ui.thumbnailView, SIGNAL("currentItemChanged(QListWidgetItem*,QListWidgetItem*)"), self.activeThumbChanged)
		self.connect(self.ui.zoomSlider, SIGNAL("valueChanged(int)"),  self.zoomImageSliderChanged)

	def zoomImageSliderChanged(self, val):
		#print val
		pass
	
	def setZoomSliderPosition(self, val):
		self.ui.zoomSlider.setValue(val)

	def setupThumbViewer(self):
		w = Config.THUMBWIDTH
		self.ui.thumbnailView.setIconSize(QSize(w,w))
		self.ui.thumbnailView.setGridSize(QSize(w+50,w+50))


	def openImagesFileDialog(self):
		files = QFileDialog.getOpenFileNames(self, 'Open image files', '.', Config.IMAGE_FILE_FILTER_STRING)
		if files:
			images = [Image(file, self.repo) for file in files]
			self.ui.thumbnailView.addImages(images)

	def openImagesRecursiveFileDialog(self):
		dir = QFileDialog.getExistingDirectory(self, 'Open images recursively', '.', QFileDialog.ShowDirsOnly)

	def activeThumbChanged(self ,current, previous):
		"""currentItemChanged(QListWidgetItem*,QListWidgetItem*)"""
		c = p = "None"
		if current: c = current.path
		if previous: p = previous.path
		print "current: ", c, "previous: ", p
		#pixmap = QPixmap(current.path)
		#self.ui.imageView.setPixmap(pixmap)
		self.ui.imageView.setImage(current.path)




if __name__ == "__main__":
	app = QApplication(sys.argv)
	window = MainGui()
	window.show()
	sys.exit(app.exec_())

